package com.example.index.reflect

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.cat.annotation.Index
import com.example.index.R
import com.example.index.adapter.foo.Foo

@Index(keywords = "反射创建列表/rawType/ViewHolder", link = "")
class TestListActivity : BaseListActivity<Foo, TestListActivity.FooViewHolder>() {
    override fun getLayoutRes() = R.layout.demo_item


    var dataIndex = 0

    val onItemClickListener = View.OnClickListener {

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        onLoadMoreData(
            createListData()
        )
    }

    private fun createListData(): List<Foo> {
        val res = arrayListOf<Foo>()
        val start = dataIndex
        val end = dataIndex + 10
        dataIndex = end
        for (i in start until end) {
            res.add(Foo("Item $i"))
        }
        return res

    }


    override fun startLoadMore() {
        window.decorView.postDelayed({
            val createListData = createListData()
            onLoadMoreData(createListData)
        }, 1800)
    }

    override fun startRefresh() {
        window.decorView.postDelayed({
            dataIndex = 0
            val createListData = createListData()
            onRefreshData(createListData)
        }, 1800)
    }


    override fun onBindViewHolder(holder: FooViewHolder, item: Foo) {
        holder.name.text = item.value

        holder.name.setOnClickListener(onItemClickListener)

    }

    class FooViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val name = itemView.findViewById<TextView>(R.id.item_name)

    }
}
